package com.wetao.module_user.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.wetao.lib_common.EventBus.EventBusMessage;
import com.wetao.lib_common.base.BaseLazyFragment;
import com.wetao.lib_common.bean.response.Loginbean_CoachClass;
import com.wetao.lib_common.utils.BaseEvent;
import com.wetao.lib_common.utils.GsonUtil;
import com.wetao.lib_common.utils.JsonUtils;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.view.OnMultiClickListener;
import com.wetao.module_user.R;
import com.wetao.module_user.R2;
import com.wetao.module_user.adapter.ClassRecyclerAdapter;
import com.wetao.module_user.contract.UserClassTeachFragmentCon;
import com.wetao.module_user.presenter.UserClassTeachFragmentPre;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;

public class UserClassTeachFragment extends BaseLazyFragment<UserClassTeachFragmentCon.View, UserClassTeachFragmentPre> implements
        UserClassTeachFragmentCon.View {

    //@BindView(R2.id.lrv_class_list)
    LRecyclerView lrvClassList;
    AppCompatTextView tvAddCoachClass;
    private LRecyclerViewAdapter lrvAdapter;
    private RelativeLayout rl_no_data;

    private List<Loginbean_CoachClass> mCoachClasses;
    private ClassRecyclerAdapter mClassRecyclerAdapter;

    public static UserClassTeachFragment newInstance(String classes) {
        Bundle args = new Bundle();
        args.putString("classes", classes);

        UserClassTeachFragment fragment = new UserClassTeachFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.user_fragment_teach_class;
    }

    @Override
    public UserClassTeachFragmentPre createPresenter() {
        return new UserClassTeachFragmentPre(mContext);
    }

    @Override
    public UserClassTeachFragmentCon.View createView() {
        return this;
    }

    @Override
    public void init(View rootView) {
        lrvClassList = rootView.findViewById(R.id.lrv_class_list);
        rl_no_data = rootView.findViewById(R.id.rl_manager_noData);
        tvAddCoachClass = rootView.findViewById(R.id.tv_add_coach_class);
        tvAddCoachClass.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if(null != onListenClick) {
                    onListenClick.onListenClick();
                }
            }
        });
        Bundle bundle = null;
        if ((bundle = getArguments()) != null) {
            String classes = bundle.getString("classes");
            if (!TextUtils.isEmpty(classes)) {
                mCoachClasses = GsonUtil.GsonToList(classes, Loginbean_CoachClass.class);
            }
        }

        //if(null != mCoachClasses && mCoachClasses.size() > 0)
        {
            initAdapter();
        }
    }

    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        lrvClassList.setLayoutManager(linearLayoutManager);
        lrvClassList.setNestedScrollingEnabled(false);
        lrvClassList.setOverScrollMode(View.OVER_SCROLL_NEVER);
        lrvClassList.setPullRefreshEnabled(false);
        //lrvClassList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mClassRecyclerAdapter = new ClassRecyclerAdapter(getActivity(), mCoachClasses);
        //lrvClassList.setAdapter(mClassRecyclerAdapter);

        mClassRecyclerAdapter.setOnItemClickListener(new ClassRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClickSwitch(int position, List<Loginbean_CoachClass> listModel) {
                if(null != onListenClick) {
                    onListenClick.onItemClickSwitch(listModel.get(position));
                }
            }

            @Override
            public void onItemClickExit(int position, List<Loginbean_CoachClass> listModel) {
                if(null != onListenClick) {
                    onListenClick.onItemClickExit(listModel.get(position));
                }
            }
        });

        lrvAdapter = new LRecyclerViewAdapter(mClassRecyclerAdapter);
        lrvClassList.setEmptyView(rl_no_data);
        lrvClassList.setAdapter(lrvAdapter);
    }

    @Override
    public void onFragmentFirst() {
        super.onFragmentFirst();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onFragmentInVisible() {
        super.onFragmentInVisible();
    }

    @Override
    public void onFragmentVisble() {
        super.onFragmentVisble();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void busGetMessage(EventBusMessage messageEvent) {
        if(messageEvent.getAction() == EventBusMessage.EVENT_BUS_ACTION_UPDATE_JOIN_EXIT_CLASS_COACH) {
            mCoachClasses = GsonUtil.GsonToList(messageEvent.getMsg(), Loginbean_CoachClass.class);
            if(null != lrvAdapter) {
               // lrvAdapter.setData(mCoachClasses);
                mClassRecyclerAdapter.setData(mCoachClasses);
                lrvClassList.setEmptyView(rl_no_data);
                lrvAdapter.notifyDataSetChanged();
            } else {
                initAdapter();
            }
        }
    }

    public interface OnListenClick {
        void onListenClick();
        void onItemClickSwitch(Loginbean_CoachClass coachClass);
        void onItemClickExit(Loginbean_CoachClass coachClass);
    }
    OnListenClick onListenClick;

    public void setOnListenClick(OnListenClick onListenClick) {
        this.onListenClick = onListenClick;
    }
}
